//
//  STFindDoorListVC.m
//  YiYueHairdresser
//
//  Created by zhouzhouzhou on 2018/10/9.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STFindDoorListVC.h"
#import "STDoorInfoVC.h"

#import "DOPDropDownMenu.h"
#import "STHairHouseListView.h"
#import "STDoorFilterView.h"
#import "STLinkageTable.h"
#import "STGetCityArea.h"
#import "STSearchVC.h"

@interface STFindDoorListVC ()< DOPDropDownMenuDataSource, DOPDropDownMenuDelegate,STHairHouseListViewDelegate,STFilterViewDelegate,STLinkageTableDelegate>{
    NSArray * filterCondition;//筛选条件
    NSMutableArray * arrDoors;//搜索美发师
    NSMutableArray * arrLocalData;
    DOPDropDownMenu * selMenu ;
    STHairHouseListView * findDoorListView;
    NSMutableArray * arrFinds;
    NSMutableArray *arrMenuRows;
    int filterType;  //筛选类型1-附近，2-条件 3-筛选，
    int pageNum;
    int sizeNum;
    long distance; //附近距离
    int cityId;
    int provinceId;
    int districtId;  //区域ID
    NSString * coupon;//优惠券类型 1 满减 2 折扣
    NSString * position;//: 职级 ,
    int sortType;// 0综合，1距离最近，2月接单最多，3评论量最多，4，价格最低，5价格最高 ,
}

@property (nonatomic,strong)NSArray *categoryIDArray;
@property (nonatomic,strong)NSMutableDictionary *addressInfoParmas;
@end

@implementation STFindDoorListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self getAllCityAearInfo];
    
    self.navigationItem.title = @"入驻申请";
    [self wr_setNavBarTintColor:[UIColor blackColor]];
     UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(actionFindDoor)];
    self.navigationItem.rightBarButtonItem = rightBarBtn;
    self.view.backgroundColor = BgColor;
    
    filterCondition = @[ @"综合", @"距离最近", @"月接单最多", @"评论量最多"];// , @"价格最低", @"价格最高"
    arrMenuRows = [NSMutableArray arrayWithArray:@[ @[ @"附近", @"民治", @"北站" , @"白石龙"], filterCondition, @[ @"筛选"]]];
    [self loadFindDoorList];
    
    [self loadMenuListView];
    
    // 默认值请求
    self.categoryIDArray = @[];
    cityId = 0;
    districtId = 0;
    provinceId = 0;
    distance = 0;
    sortType = 0;
    [self.addressInfoParmas setObject:@0 forKey:@"provinceId"];
    [self.addressInfoParmas setObject:@0 forKey:@"cityId"];
    [self.addressInfoParmas setObject:@0 forKey:@"districtId"];
    
    [self sortSearchNetworkRequest];
}

//网络请求 门店 数据加载
-(void)actionFindDoor{
    STSearchVC * hairInfoVC  =[STSearchVC new];
    hairInfoVC.hidesBottomBarWhenPushed= YES;
    [self.navigationController pushViewController:hairInfoVC animated:YES];
    
}

#pragma mark -界面

-(void)loadMenuListView{
    
    selMenu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, 64) andHeight:40];
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    view.backgroundColor = [UIColor orangeColor];
    STDoorFilterView * filterView = [[STDoorFilterView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    [filterView setHairerFilterView];
    filterView.delegate = self;
    
    STLinkageTable * tableView = [[STLinkageTable alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    tableView.isRelate = YES;
    tableView.delegate = self;
    [STMainPageService getCityAreas:^(id result, NSError *error) {
        if (error) {
            [ZYHCommonMethod showError:error];
        }
        NSArray * arrModels = [NSArray modelArrayWithClass:[STCityInfoModel class] json:result];
        STCityInfoModel * model = arrModels[0];
        cityId =model.parentId;
//        [self actionFindDoor];
        arrLocalData = (NSMutableArray *)[self addNearbyData:arrModels];;
        tableView.dataArray =arrLocalData;
        [tableView loadLinkageTableView];
    }];
    
    
    selMenu.isAddOtherView = YES;
    selMenu.otherView = @{@"columnNum0":tableView,@"columnNum2":filterView};
    selMenu.dataSource = self;
    selMenu.delegate = self;
    [self.view addSubview:selMenu];
    
}


-(void)loadFindDoorList{
    findDoorListView = [[STHairHouseListView alloc]initWithFrame:CGRectMake(0, 64+40, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    findDoorListView.delegate = self;
//    [findDoorListView setHairHouseListView:0 data:@[@"",@"",@"",@"",@""]];
    [self.view addSubview:findDoorListView];
}


-(NSMutableArray *)addNearbyData:(NSArray *)oldArr{
    NSMutableArray * newArr = [NSMutableArray arrayWithArray:oldArr];
    NSMutableArray * arrNearby = [NSMutableArray arrayWithCapacity:5];
    for (int i= 0; i<6; i++) {
        STCityInfoModel * model = [[STCityInfoModel alloc]init];
        model.name = i==0?@"0.5km":[NSString stringWithFormat:@"%dkm",i];
        model.areaId= i==0?0.5:i;
        [arrNearby addObject:model];
    }
    STCityInfoModel * model = [[STCityInfoModel alloc]init];
    model.name = @"附近";
    model.areaList =(NSArray<STCityInfoModel> *)arrNearby;
    [newArr insertObject:model atIndex:0];
    if (newArr.count>1) {
        STCityInfoModel * model2 = newArr[1];
        model2.name = @"全部区域";
    }
    return newArr;
}


#pragma mark - ColumnsInMenu view data source
- (NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu {
    return arrMenuRows.count;
}

- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column {
    NSArray * arr = arrMenuRows[column];
    return arr.count;
}

- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath {
    NSArray * arr = arrMenuRows[indexPath.column];
    return arr[indexPath.row];
}

#pragma mark - DOPDropDownMenuDelegate
- (void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath {

    sortType = indexPath.row;
    
    [self sortSearchNetworkRequest];
}

- (void)menu:(DOPDropDownMenu *)menu didSelectCalender:(NSString *)calender {
    
//    [self loadFindDoorList];
}
- (void)menu:(DOPDropDownMenu *)menu numberOfSectionAtIndexPath:(NSInteger)section{
    
}

-(void)hairHouseListSelNum:(NSInteger)selNum;{
    STDoorInfoVC * doorInfoVC  =[STDoorInfoVC new];
    doorInfoVC.isSign = NO;
    doorInfoVC.stylistId = 1;
    doorInfoVC.hidesBottomBarWhenPushed= YES;
    [self.navigationController pushViewController:doorInfoVC animated:YES];
}

//筛选回调
-(void)sureFilterCondition:(NSDictionary *)filterDic{
    [selMenu dismiss];
    filterType = 3;
    position = filterDic[@"grade"];
    coupon = filterDic[@"active"];
    
    NSString *itemID = [NSString stringWithFormat:@"%@",[filterDic objectForKey:@"itemid"]];
    self.categoryIDArray = @[itemID];
    
//    if(position.length || coupon.length ){
//        [self actionFindDoor];
//    }
    
    [self sortSearchNetworkRequest];
    
    [selMenu dismiss];
}


//二级联动table
-(void)linkageTableView:(UITableView *)view indexPath:(NSIndexPath*)indexPath{
    filterType = 1;
    STCityInfoModel * model = arrLocalData[indexPath.section];
    if (indexPath.section ==0) {
        STCityInfoModel * distanceModel = model.areaList[indexPath.row];
        distance =distanceModel.areaId;
        cityId = 0;
        districtId = 0;
        provinceId = distanceModel.provinceId;
    }else if (indexPath.section ==1) {
        STCityInfoModel * distanceModel = model.areaList[1];
        cityId = distanceModel.parentId;
        distance = 0;
        districtId = 0;
        provinceId = distanceModel.provinceId;
    }else{
        STCityInfoModel * distanceModel = model.areaList[indexPath.section];
        districtId = (int)distanceModel.parentId;
        cityId = 0;
        distance = 0;
        provinceId = distanceModel.provinceId;
    }
//    [self actionFindDoor];
    [selMenu dismiss];
}

#pragma mark - 筛选请求
-(void)sortSearchNetworkRequest
{
    [STHairService relieveStoreWithCategoryIds:self.categoryIDArray
                                        cityId:cityId
                                    districtId:districtId
                                   maxDistance:0
                                    provinceId:provinceId
                                      sortType:sortType
                                    completion:^(id result, NSError *error) {
                                        [self getSearchResultResponse:result];
                                    }];
}

-(void)getSearchResultResponse:(id)result
{
    if (!result) {
        return;
    }
    NSArray * arrModels = [NSArray modelArrayWithClass:[STHairHouseListModel class] json:result];
    [findDoorListView setHairHouseListView:0 data:arrModels];
}

-(void)getAllCityAearInfo
{
    NSString *plistPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"AreaPlist.plist"];
     NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:plistPath];
    
//    __block NSArray *tempArray = [NSArray modelArrayWithClass:[STProvinceModel class] json:array];
    
    [[STFirstControl sharedInstance] setFirstControl];
    [STFirstControl sharedInstance].locationSuccess = ^(AMapLocationReGeocode *selfRegeocode) {
        [self handlewithAddressInfo:selfRegeocode array:array];
    };
}

-(void)handlewithAddressInfo:(AMapLocationReGeocode *)selfRegeocode array:(NSMutableArray *)array
{
    NSString *provinceName = [STFirstControl sharedInstance].selfRegeocode.province;
    NSString *cityName = [STFirstControl sharedInstance].selfRegeocode.city;
    NSString *districtName = [STFirstControl sharedInstance].selfRegeocode.district;
    
    // 省
    NSDictionary *provinceDic = [self queryIndexAddressInfo:array matchName:provinceName];
    
    // 市
    NSArray *cityArray = [provinceDic objectForKey:@"areaList"];
    NSDictionary *cityDic = [self queryIndexAddressInfo:cityArray matchName:cityName];
    
    // 区
    NSArray *areaArray = [cityDic objectForKey:@"areaList"];
    NSDictionary *areaDic = [self queryIndexAddressInfo:areaArray matchName:districtName];
    
    if (provinceDic) {
        [self.addressInfoParmas setObject:[provinceDic objectForKey:@"areaId"] forKey:@"provinceId"];
    }
    if (cityDic) {
        [self.addressInfoParmas setObject:[cityDic objectForKey:@"areaId"] forKey:@"cityId"];
    }
    if (areaDic) {
        [self.addressInfoParmas setObject:[areaDic objectForKey:@"areaId"] forKey:@"districtId"];
    }
    DLog(@"self.addressInfoParmas==%@",self.addressInfoParmas);
}
// 查询定位对应的ID
-(NSDictionary *)queryIndexAddressInfo:(NSArray *)array matchName:(NSString *)name
{
    __block NSDictionary *resultDic;
    for (NSDictionary *searchDic in array) {
        [searchDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            if ([key isEqualToString:@"name"]) {
                if ([obj isEqualToString:name]) {
                    resultDic = searchDic;
                }
            }
        }];
    }
    return resultDic;
}
-(NSMutableDictionary *)addressInfoParmas
{
    if (!_addressInfoParmas) {
        _addressInfoParmas = [NSMutableDictionary dictionary];
    }
    return _addressInfoParmas;
}

@end
